﻿using OestsCommon.TcpNet;
using OestsServer.Net;

namespace OestsServer.Handlers
{
	public class JoinRoomHandler : HandlerBase
	{
		public override PacketKey PKey
		{
			get
			{
				return PacketKey.JoinRoom;
			}
		}

		public override void Execute(ClientChannel client, Request packet)
		{
			////接收到数据解析：房间id 密码
			//Struct2<int, string> roomIdPwd = JsonMapper.ToObject<Struct2<int, string>>(packet.DataStr);
			//#region 得到房间
			//foreach (var room in RoomManager.Instance.RoomList)
			//{
			//	if (room.RoomId == roomIdPwd.Value1)
			//	{
			//		Struct2<Room, Student> backData = new Struct2<Room, Student>();
			//		//验证密码
			//		if (room.RoomPassword == roomIdPwd.Value2)
			//		{
			//			if (room.StudentTemps.Count == room.DicPostIdStudentId.Count)
			//			{
			//				// 加入失败 仅向当前用户返回密码错误的消息
			//				ResponsePacket.ReturnCode = ReturnCode.Fail;
			//				ResponsePacket.RetrunMessage = "加入房间失败，房间已满！";
			//				ResponsePacket.DataObject = room;
			//				client.Send(ResponsePacket);
			//				return;
			//			}
			//			else
			//			{
			//				//加入成功 房间所有成员广播
			//				backData = new Struct2<Room, Student>(room, client.User as Student);

			//				if (!room.StudentTemps.Contains(client.User.DBid))
			//				{
			//					room.StudentTemps.Add(client.User.DBid);
			//				}
			//				ResponsePacket.DataObject = backData;
			//				ResponsePacket.ReturnCode = ReturnCode.Success;
			//				BroadcastInRoom(room, ResponsePacket);
			//				return;
			//			}
			//		}
			//		else
			//		{
			//			// 加入失败 仅向当前用户返回密码错误的消息
			//			ResponsePacket.ReturnCode = ReturnCode.Fail;
			//			ResponsePacket.RetrunMessage = "加入房间失败，密码错误！";
			//			ResponsePacket.DataObject = room;
			//			client.Send(ResponsePacket);
			//			return;
			//		}
			//	}
			//}
			//#endregion
			//#region 房间不存在
			////房间不存在
			//ResponsePacket.ReturnCode = ReturnCode.Fail;
			//ResponsePacket.RetrunMessage = "加入房间失败，房间不存在！";
			//ResponsePacket.DataObject = null;
			//client.Send(ResponsePacket);
			//return;
			//#endregion
		}
	}
}
